//
//  HTGroupViewController.m
//  HeartTravel
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 tantan. All rights reserved.
//

#import "HTGroupViewController.h"
#import "HTGroupTableViewCell.h"
#import "PKRequestManager.h"
#import "HTGroupModel.h"
#import "HTGroupWebViewController.h"
#import "HTGroupTableHeaderView.h"
#define kURL @"http://api.daizhe.cn/v2_4/share?ac=list&device_type=2&cur_version=2.3.5.1&current_date=1445654107&device_code=77FEFD37-68DD-413D-8097-32017A5C6634&device_token=21093eff1a82bf7778421dddfcf93b2e&dz_token=&dz_uid=&order=0&type=6"
@interface HTGroupViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *mainListTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) HTGroupTableHeaderView *headerView;
@end

@implementation HTGroupViewController
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)requestDataSource
{
    [PKRequestManager requestWithType:GET urlString:kURL parDic:nil finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in dict[@"responseData"][@"list"]) {
            HTGroupModel *model = [[HTGroupModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainListTableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"%@ 社区界面数据请求失败", error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self requestDataSource];
    
    self.navigationController.navigationBar.hidden = YES;
    self.titleLabel.text = @"社区";
    [self.rightButton setBackgroundImage:[UIImage imageNamed:@"搜索"] forState:(UIControlStateNormal)];
    
    _mainListTableView = [[UITableView alloc]init];
    _mainListTableView.frame = CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT - 70);
    _mainListTableView.dataSource = self;
    _mainListTableView.delegate =self;
    [_mainListTableView registerClass:[HTGroupTableViewCell class] forCellReuseIdentifier:@"HTGroupTableViewCell"];
    _mainListTableView.rowHeight = 260;
    
    _headerView = [[HTGroupTableHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    _headerView.mainScrollView.delegate = self;
    self.mainListTableView.tableHeaderView = _headerView;
    
    [self.view addSubview:_mainListTableView];
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}

- (void)nextImage
{
    if (_headerView.pageControl.currentPage == 3) {
        _headerView.pageControl.currentPage = 0;
    }else{
        _headerView.pageControl.currentPage++;
    }
    CGPoint offSet = CGPointMake(_headerView.pageControl.currentPage * SCREEN_WIDTH, 0);
    [_headerView.mainScrollView setContentOffset:offSet animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _headerView.pageControl.currentPage = scrollView.contentOffset.x / SCREEN_WIDTH;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.dataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HTGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HTGroupTableViewCell" forIndexPath:indexPath];
    // Configure the cell...
    cell.configureModel = self.dataSourceArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HTGroupModel *model = self.dataSourceArray[indexPath.row];
    HTGroupWebViewController *webVC = [[HTGroupWebViewController alloc]init];
    webVC.share_id = model.share_id;
    [self.navigationController pushViewController:webVC animated:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
